package Presentation;

import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public class SpinnerColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {

    private final JSpinner[] js;
    
    public SpinnerColumn(JTable table, int column){
        js = new JSpinner[table.getRowCount()];
        for(int i = 0 ; i < table.getRowCount() ; i++){
            js[i] = new JSpinner(new SpinnerNumberModel(0, 0, null, 1));
        }
        
        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(column).setCellRenderer(this);
        columnModel.getColumn(column).setCellEditor(this);
    }
    
    @Override
    public Object getCellEditorValue() {
        return "";
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        return js[row];
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return js[row];
    }

    public Object getValue(int row){
        return js[row].getValue();
    }
    
}
